
<!-- Danh sách sẽ có phân trang -->
<?php
	include_once "../classes/giaovien_bs.php";
	include_once "../classes/hopdong_bs.php";
	include_once "../classes/giaovienvaitro_bs.php";
	include_once "../classes/chuongtrinhhoc_bs.php";
	include_once "../classes/phieuchi_bs.php";
	include_once "../classes/lophoc_bs.php";
	
	$MaKhoa = trim($_GET['mkh']);
	$Keyword = trim($_GET['key']);
	$Page    = trim($_GET['pag']);
	
	$HopDong = new hopdong_bs();
	$HopDong->SetMaLopHoc($Keyword);
	if($MaKhoa == "")
	{
		$DHopDong = $HopDong->LayDanhSach();	
		$Limit = 20; //Element Per Page
		$SoTrang = ceil(count($DHopDong) / $Limit);
		$DHopDong = $HopDong->LayDanhSachPaging(($Page - 1) * $Limit, $Limit);
	}
	else
	{
		$DHopDong = $HopDong->HopDongTheoKhoa($MaKhoa);	
		$Limit = 20; //Element Per Page
		$SoTrang = ceil(count($DHopDong) / $Limit);
		$DHopDong = $HopDong->HopDongTheoKhoaPaging($MaKhoa, ($Page - 1) * $Limit, $Limit);
	}
	
?>
<div class="Paging">
	<?php
		if($DHopDong != null)
		{
			if($Page > 1)
				echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLBangLuong(\"$MaKhoa\",\"$Keyword\",1);'>Đầu</a>";	
			for($i = 1; $i <= $SoTrang; $i++)
			{
				if($Page - 5 <= $i && $i <= $Page + 5)
				{
					if($i == $Page)
						echo "<a class='AjaxPageCur' href='#' onclick='javascript:loadXMLBangLuong(\"$MaKhoa\",\"$Keyword\",$i);'>$i</a>";
					else
						echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLBangLuong(\"$MaKhoa\",\"$Keyword\",$i);'>$i</a>";
				}
				elseif($i == $Page + 6)
					echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLBangLuong(\"$MaKhoa\",\"$Keyword\",$i);'>...</a>";
				elseif($i == $Page - 6)
					echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLBangLuong(\"$MaKhoa\",\"$Keyword\",$i);'>...</a>";
			}
			if($Page < $SoTrang)
				echo "<a class='AjaxPage' href='#' onclick='javascript:loadXMLBangLuong(\"$MaKhoa\",\"$Keyword\",$SoTrang);'>Cuối</a>";	
			
			echo "<span style='float:right; background-color:#4ABDF4; color: #FFF; padding:0px 3px'>Trang $Page/$SoTrang</span>";
		}
	?>
</div>
<div class="TableList">
	
	<div id="result">
		<table width="100%" class="DSHD">
			<tr>
				<th class="first">STT</th>
				<th width="70px">Mã HĐ</th>
				<th colspan="2">Giáo viên</th>
				<th >Lớp dạy</th>
				<th >Vai trò</th>
				<th >Ngày ký</th>
				<th >Đã nhận</th>
				<th >Tổng lương</th>
				<th >Lập <br /> phiếu chi</th>
			</tr>
			<!-- Lấy danh sách học viên từ cơ sở dữ liệu -->
			<?php
				if($DHopDong == null)
				{
					echo '<tr><td colspan="11" align="center"> Nội dung rỗng</td></tr>';
				}
				else
				{
					for($i = 0; $i < count($DHopDong); $i++)
					{
						$GiaoVien = new giaovien_bs();
						$GiaoVien->SetMaGiaoVien($DHopDong[$i]['MaGiaoVien']);
						$DGiaoVien = $GiaoVien->LayDanhSach();
						
						$GiaoVienVaiTro = new giaovienvaitro_bs();
						$GiaoVienVaiTro->SetMaVaiTro($DHopDong[$i]['MaVaiTro']);
						$DGiaoVienVaiTro = $GiaoVienVaiTro->LayDanhSach();
						
						$TGV = $DGiaoVienVaiTro == null ? " - " : $DGiaoVienVaiTro[0]['TenVaiTro'];
						
						$HT = TachHoTen($DGiaoVien[0]['HoTen']);
						
						$LopHoc = new lophoc_bs();
						$LopHoc->SetMaLopHoc($DHopDong[$i]['MaLopHoc']);
						$DLopHoc = $LopHoc->LayDanhSach();
						
						$PhieuChi = new phieuchi_bs();
						$PhieuChi->SetMaHopDong($DHopDong[$i]['MaHopDong']);
						$DPhieuChi = $PhieuChi->LayDanhSach();
						$DaNhan = 0;
						if($DPhieuChi != null)
						{
							for($j = 0; $j < count($DPhieuChi); $j++)
							{
								$DaNhan += $DPhieuChi[$j]['SoTien'];
							}
						}
						
						$ChuongTrinhHoc = new chuongtrinhhoc_bs();
						$ChuongTrinhHoc->SetMaChuongTrinh($DLopHoc[0]['MaChuongTrinh']);
						$DChuongTrinhHoc = $ChuongTrinhHoc->LayDanhSach();
						
						$TongLuong = $DChuongTrinhHoc[0]['SoTiet'] * $DHopDong[$i]['LuongMoiTiet'];
						
						$LinkAdd = 'index.php?act=phieuchi_them&mhd=' . $DHopDong[$i]['MaHopDong'] . '&pin=' . $DHopDong[$i]['PIN'] . '&luong=' . ($TongLuong - $DaNhan) . '&mgv=' . $DHopDong[$i]['MaGiaoVien'];
						
						echo '<tr class="ButtonDetails">';
							echo '<td align="center"><b>' . (($Page - 1) * $Limit + ($i + 1)) . '</b></td>';
							echo '<td align="center">' . $DHopDong[$i]['MaHopDong'] . '</td>';
							echo '<td>' . $HT[0] . '</td>';
							echo '<td>' . $HT[1] . '</td>';
							echo '<td>' . $DHopDong[$i]['MaLopHoc'] . '</td>';
							echo '<td>' . $TGV . '</td>';
							echo '<td  align="center">' . ConvertDate($DHopDong[$i]['NgayKy']) . '</td>';
							echo '<td align="right">' . number_format($DaNhan, 0, ',', ' ') . '</td>';
							echo '<td align="right">' . number_format($TongLuong, 0, ',', ' ') . '</td>';
							
							echo '<td align="center">' . ($TongLuong > $DaNhan ?  '<a href="' . $LinkAdd . '"><img src="../data/add_hopdong.png" alt="" /></a>' : '' ) . '</td>';
						echo '</tr>';
					}
				}
			?>
		</table>
	</div>
	
</div>